# -*- coding: utf-8 -*-
from match import Extractor
import os.path
import regex as re


# 伤害程度
# 致人死亡或者以特别残忍手段致人重伤造成严重残疾


class GuYiShangHaiExtractor(Extractor):
    """
    就结果的严重程度而言，有3种形态，即轻伤、重伤或死亡。
    如果没有造成轻伤以上的伤害如没有达到伤害等级或虽达到等级却属轻微伤，
    则不能以故意伤害罪论处。
    """

    @staticmethod
    def is_my_job(file_path):
        return "故意伤害" in file_path

    @staticmethod
    def extract(text):
        result = {}
        shang_hai_cheng_du = re.search(r"(轻伤)", text) is not None
        result["致人轻伤"] = shang_hai_cheng_du
        shang_hai_cheng_du = re.search(r"(重伤)", text) is not None
        result["致人重伤"] = shang_hai_cheng_du
        can_ji_cheng_du = re.search(r"(残疾|致残|伤残)", text) is not None
        result["致人残疾"] = can_ji_cheng_du
        shang_hai_cheng_du = re.search(r"(死亡|致死)", text) is not None
        result["致人死亡"] = shang_hai_cheng_du
        # 谅解
        liang_jie = re.search(r'谅解', text) is not None
        result["被害人谅解"] = liang_jie
        ji_ji_pei_chang = re.search(r'积极赔偿', text) is not None
        result["积极赔偿"] = ji_ji_pei_chang

        return result


if __name__ == '__main__':
    from match import *
    from storage import *
    import json
    import os

    data_util = DataUtil("E:/project/yaosu_law/pro_yaosu/故意伤害罪")
    extractors = [GuYiShangHaiExtractor]

    file_path, text = data_util.next_line()
    count_dict = {}
    count_key_list = ["致人轻伤", "致人重伤", "致人残疾", "致人死亡", '被害人谅解', "积极赔偿"]
    while text:
        text_json = json.loads(text)
        if file_path not in count_dict:
            count_dict[file_path] = {"count": 0}
        for extractor in extractors:
            if extractor.is_my_job(file_path):
                result = extractor.extract("\n".join(text_json["content"]))
                for key in count_key_list:
                    result_value = result[key]
                    if (type(result_value) == int or type(result_value) == float):
                        if key in count_dict[file_path]:
                            count_dict[file_path][key] = count_dict[file_path][key] + result_value
                        else:
                            count_dict[file_path][key] = result_value
                    else:
                        if key in count_dict[file_path]:
                            if result_value in count_dict[file_path][key]:
                                count_dict[file_path][key][result_value] = count_dict[file_path][key][result_value] + 1
                            else:
                                count_dict[file_path][key][result_value] = 1
                        else:
                            count_dict[file_path][key] = {}
                            count_dict[file_path][key][result_value] = 1
        count_dict[file_path]["count"] = count_dict[file_path]["count"] + 1
        file_path, text = data_util.next_line()
    for key in count_dict:
        print(key)
        count = count_dict[key]["count"]
        for ckey in count_key_list:
            ckey_result = count_dict[key][ckey]
            if (type(ckey_result) == int or type(ckey_result) == float):
                print(str(ckey) + ":%f" % (ckey_result / float(count)))
            else:
                for cckey in ckey_result:
                    print(str(ckey) + "_" + str(cckey) + ":%f" % (ckey_result[cckey] / float(count)))
    print(count_dict)
